package com.qinhao.loveCrowdFunding.service;

import java.util.List;

import com.qinhao.loveCrowdFunding.mapper.UserLoginMapper;
import com.qinhao.loveCrowdFunding.pojo.*;
import com.qinhao.loveCrowdFunding.req.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinhao.loveCrowdFunding.mapper.DetailInformationMapper;
import com.qinhao.loveCrowdFunding.mapper.SimpleInformationMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SimpleInformationService {

    @Autowired
    private SimpleInformationMapper simpleInformationMapper;
    @Autowired
    private DetailInformationMapper detailInformationMapper;
    @Autowired
    private UserLoginMapper userLoginMapper;

    public SimpleInformationDTO getSimpleInformation(SimpleInfoRequest simpleInfoRequest) throws Exception {
        PageHelper.startPage(simpleInfoRequest.getCurrentPage(), simpleInfoRequest.getPageSize());
        List<SimpleInformationBO> simpleInformationBOList = simpleInformationMapper.getAllSimpleInformation();
        PageInfo<SimpleInformationBO> pageInfo = new PageInfo<>(simpleInformationBOList);
        PageFormation pageFormation = new PageFormation();
        pageFormation.setCurrentPage(pageInfo.getPageNum());
        pageFormation.setCurrentSize(simpleInformationBOList.size());
        pageFormation.setTotalSize(pageInfo.getTotal());
        pageFormation.setTotalPage(pageInfo.getPages());
        SimpleInformationDTO simpleInformationDTO = new SimpleInformationDTO();
        if (simpleInformationBOList != null) {
            simpleInformationDTO.setList(simpleInformationBOList);
        }
        simpleInformationDTO.setPageFormation(pageFormation);
        return simpleInformationDTO;
    }

    public TelephoneNumberDTO getTelephoneNumber(int userID) {
        TelephoneNumberBO telephoneNumber = simpleInformationMapper.getTelephoneNumber(userID);
        TelephoneNumberDTO telephoneNumberDTO = new TelephoneNumberDTO();
        if (telephoneNumber != null) {
            BeanUtils.copyProperties(telephoneNumber, telephoneNumberDTO);
        }
        return telephoneNumberDTO;
    }

    public DeleteAllowDTO isAllowDelete(DeleteMySimpleRequest deleteMySimpleRequest) {
        DeleteAllowDTO deleteAllowDTO = DeleteAllowDTO.builder().build();
        SimpleInformationStatusBO simpleInformationStatus =
                simpleInformationMapper.getSimpleInformationStatus(deleteMySimpleRequest.getId());
        if (simpleInformationStatus.getStatus() != 0) {
            deleteAllowDTO.setAllowDelete(false);
        } else {
            deleteAllowDTO.setAllowDelete(true);
        }
        return deleteAllowDTO;
    }

    @Transactional
    public DeleteMySimpleDTO deleteSimpleAndDetail(DeleteMySimpleRequest deleteMySimpleRequest) {
        DeleteMySimpleDTO deleteMySimpleDTO = DeleteMySimpleDTO.builder().build();
        simpleInformationMapper.deleteSimpleInformationById(deleteMySimpleRequest);
        detailInformationMapper.deleteDetailInformationById(deleteMySimpleRequest.getId());
        deleteMySimpleDTO.setId(deleteMySimpleRequest.getId());
        return deleteMySimpleDTO;
    }

    public SimpleInformationDTO getMySimpleInformation(SimpleInfoRequest simpleInfoRequest) throws Exception {
        PageHelper.startPage(simpleInfoRequest.getCurrentPage(), simpleInfoRequest.getPageSize());
        List<SimpleInformationBO> simpleInformationBOList = simpleInformationMapper.getMySimpleInformation(simpleInfoRequest);
        PageInfo<SimpleInformationBO> pageInfo = new PageInfo<>(simpleInformationBOList);
        PageFormation pageFormation = new PageFormation();
        pageFormation.setCurrentPage(pageInfo.getPageNum());
        pageFormation.setCurrentSize(simpleInformationBOList.size());
        pageFormation.setTotalSize(pageInfo.getTotal());
        pageFormation.setTotalPage(pageInfo.getPages());
        SimpleInformationDTO simpleInformationDTO = new SimpleInformationDTO();
        if (simpleInformationBOList != null) {
            simpleInformationDTO.setList(simpleInformationBOList);
        }
        simpleInformationDTO.setPageFormation(pageFormation);
        return simpleInformationDTO;
    }

    public void report(ReportRequest reportRequest) {
        // 像report表插入举报信息
        simpleInformationMapper.addReportContentBySimpleInformationId(reportRequest);
        // 更改众筹状态为2：被举报
        simpleInformationMapper.convertStatusToReport(reportRequest);
    }

    public CanReportQueryDTO canReportQuery(CanReportQueryRequest canReportQueryRequest) {
        CanReportQueryDTO canReportQueryDTO = CanReportQueryDTO.builder().build();
        CanReportQueryBO report = simpleInformationMapper.getReport(canReportQueryRequest);
        if (report != null) {
            canReportQueryDTO.setIsCanReport(0);
            return canReportQueryDTO;
        }
        CanReportQueryBO intergral = simpleInformationMapper.getIntergral(canReportQueryRequest);
        if (intergral != null && intergral.getIntergralCount() < 60) {
            canReportQueryDTO.setIsCanReport(0);
            return canReportQueryDTO;
        }
        canReportQueryDTO.setIsCanReport(1);
        return canReportQueryDTO;
    }

    public Boolean canPublish(CanPublishFundingQueryRequest canPublishFundingQueryRequest) {
        Integer canPublishCount = 0;
        GetIntegralBalanceRequest getIntegralBalanceRequest = GetIntegralBalanceRequest.builder().userId(canPublishFundingQueryRequest.getUserId()).build();
        GetIntegralBalanceBO integralBalanceByUserId = userLoginMapper.getIntegralBalanceByUserId(getIntegralBalanceRequest);
        Integer intergralCount = integralBalanceByUserId.getIntergralCount();
        if (intergralCount >= 150) {
            canPublishCount = 3;
        } else if (intergralCount < 150 && intergralCount >= 100) {
            canPublishCount = 2;
        } else {
            canPublishCount = 1;
        }
        CanPublishQueryBO unEndFunding = userLoginMapper.getUnEndFunding(canPublishFundingQueryRequest);
        if (unEndFunding.getCount() < canPublishCount) {
            return true;
        }
        return false;
    }
}
